package com.sts.transform.function;

import cn.hutool.core.date.DateUtil;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import io.github.yindz.random.RandomSource;

import java.time.LocalDate;
import java.util.Map;

/**
 * @author lsl
 * @since 2025年07月08日 19:33
 */
public class RandDateFunction extends AbstractFunction {

    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
        String startDate = FunctionUtils.getStringValue(arg1, env);
        String endDate = FunctionUtils.getStringValue(arg2, env);
        LocalDate startLocalDate = DateUtil.parse(startDate).toLocalDateTime().toLocalDate();
        LocalDate endLocalDate = DateUtil.parse(endDate).toLocalDateTime().toLocalDate();

        return new AviatorString(RandomSource.dateTimeSource().randomDate(startLocalDate, endLocalDate, "yyyy-MM-dd"));
    }

    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
        String startDate = FunctionUtils.getStringValue(arg1, env);
        String endDate = FunctionUtils.getStringValue(arg2, env);
        String dateFormat = FunctionUtils.getStringValue(arg3, env);
        LocalDate startLocalDate = DateUtil.parse(startDate).toLocalDateTime().toLocalDate();
        LocalDate endLocalDate = DateUtil.parse(endDate).toLocalDateTime().toLocalDate();

        return new AviatorString(RandomSource.dateTimeSource().randomDate(startLocalDate, endLocalDate, dateFormat));
    }

    @Override
    public String getName() {
        return "randDate";
    }
}
